package com.hd.service.personaloffice.impl;

import com.hd.dao.BaseDao;
import com.hd.entity.Todo;
import com.hd.entity.personaloffice.WorkCommentOpinion;
import com.hd.entity.sys.User;
import com.hd.service.personaloffice.WorkCommentOpinionService;
import com.hd.service.sys.UserService;
import com.hd.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @Author: hzhh123
 * @Date: 2018/12/24 22:54
 * 工作评论
 */
@Service("workCommentOpinionService")
public class WorkCommentOpinionServiceImpl implements WorkCommentOpinionService {
    @Autowired
    private BaseDao<WorkCommentOpinion,String>workCommentOpinionDao;
    @Autowired
    private UserService userService;
    @Autowired
    private BaseDao<Todo,String>todoDao;
    @Override
    public Serializable save(WorkCommentOpinion workCommentOpinion) {
        workCommentOpinion.setId(UUID.randomUUID().toString());
        return workCommentOpinionDao.save(workCommentOpinion);
    }

    @Override
    public void update(WorkCommentOpinion workCommentOpinion) {
        workCommentOpinion.setLastUpdateTime(new Date());
        workCommentOpinionDao.update(workCommentOpinion);
    }

    @Override
    public WorkCommentOpinion get(String id) {
        return workCommentOpinionDao.get(WorkCommentOpinion.class,id);
    }

    @Override
    public void delete(String id) {
        workCommentOpinionDao.delete(WorkCommentOpinion.class,id);
    }

    @Override
    public void deleteByParams(String objId,String type) {
        String hql="delete from WorkCommentOpinion where objId=? and type=?";
        workCommentOpinionDao.execute(hql,objId,type);
    }
    @Override
    public void deleteByParams(String objId,String type,String commentator) {
        String hql="delete from WorkCommentOpinion where objId=? and commentator=? and type=?";
        workCommentOpinionDao.execute(hql,objId,commentator,type);
    }

    @Override
    public List<WorkCommentOpinion> findByParams(String objId,String type) {
        String hql="from WorkCommentOpinion where objId=? and type=?";
        List<WorkCommentOpinion>vos=new ArrayList<WorkCommentOpinion>();
        vos=workCommentOpinionDao.find(hql,objId,type);
        if(vos.size()>0){
            for(WorkCommentOpinion vo:vos){
                if(StringUtils.isNotNull(vo.getCommentator())){
                    User user=userService.get(vo.getCommentator());
                    if(user!=null){
                        vo.setCommentatorName(user.getName());
                    }
                }
            }
        }
        return vos;
    }

    @Override
    public void updateByObjId(String objId, Integer timer) {
        String hql="update WorkCommentOpinion set timer=? where objId=?";
        workCommentOpinionDao.execute(hql,timer,objId);
    }
    @Override
    public void updateOpinion(String id, String opinion) {
        WorkCommentOpinion vo=get(id);
        if(StringUtils.isNotNull(vo.getOpinion())){
            if(!vo.getOpinion().equals(opinion)){
               String hql="update WorkCommentOpinion set lastUpdateTime=?,opinion=? where id=?";
               workCommentOpinionDao.execute(hql,new Date(),opinion,id);
            }
        }else{
            String hql="update WorkCommentOpinion set commentTime=?,opinion=? where id=?";
            workCommentOpinionDao.execute(hql,new Date(),opinion,id);
        }
        String hql="update Todo set status=1 where objId=? and userId=?";
        todoDao.execute(hql,vo.getObjId(),vo.getCommentator());
    }
}
